package com.heima.user.service.impl;

import com.heima.common.cache.CacheService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.user.service.UserService;
import com.heima.user.thread.AppThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class UserServiceImpl  implements UserService {

    @Autowired
    private CacheService cacheService;
    @Override
    public ResponseResult userFollow(UserRelationDto dto) {

        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);

        }
        //获得用户和id
        ApUser user = AppThreadLocalUtil.getUser();
        Integer userId = user.getId();
        if (userId == null || userId == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
        }
        //判断类型操作
        Short operation = dto.getOperation();
        Integer authorId = dto.getAuthorId();
        if (operation == 0){
            cacheService.zAdd(userId.toString(),authorId.toString(),System.currentTimeMillis());
            cacheService.zAdd(authorId.toString(),userId.toString(),System.currentTimeMillis());
        }else {
            cacheService.zRemove(userId.toString(),authorId.toString());
            cacheService.zRemove(authorId.toString(),userId.toString());


        }


        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
